package com.module.rotary.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.module.rotary.R;
import com.module.rotary.activity.RotaryActivity;

public class DialogResult extends AlertDialog {

    private Activity activity;
    private TextView tvContent;
    private TextView tvReceive;
    private TextView tvStart;
    private FrameLayout flAd;
    private ImageView imgClose;
    private ImageView imgVipOpen;
    private OnDialogClickLister clickLister;
    private String content;
    private String btnContent;
    private int pos;

    public DialogResult(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_result);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        tvContent = findViewById(R.id.tvContent);
        tvReceive = findViewById(R.id.tvReceive);
        tvStart = findViewById(R.id.tvStart);
        flAd = findViewById(R.id.flAd);
        imgClose = findViewById(R.id.imgClose);
        imgVipOpen = findViewById(R.id.imgVipopen);
        tvReceive.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        if (this.pos != 6) {
            tvContent.setText("恭喜获得" + this.content + "碎片");
        } else {
            tvContent.setText("恭喜获得再抽一次资格");
        }

        if (!TextUtils.isEmpty(this.btnContent)) {
            UMManager.onEvent(activity,"Ad2Show");
            tvStart.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            tvStart.setPadding(0,0,0,0);
            tvStart.setText(this.btnContent);
        }else {
            UMManager.onEvent(activity,"Ad5Show");
        }

        imgVipOpen.post(new Runnable() {
            @Override
            public void run() {
                int width = imgVipOpen.getWidth();
                int height = width / 16 * 9;

                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgVipOpen.getLayoutParams();
                lp.height = height;
                imgVipOpen.requestLayout();

                AdManager.getInstance().showNativeAD(activity, flAd, px2dp(activity, width), 0, AdManager.NATIVE_AD_TYPE_ACROSS_IMAGE, false);
            }
        });

        initLister();
    }

    private void initLister() {
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getTvStart().equals("继续抽奖")) {
                    UMManager.onEvent(activity,"Ad2Pay");
                    dismiss();
                    activity.startActivity(new Intent(activity, RotaryActivity.class));
                    return;
                }
                if (clickLister != null) {
                    clickLister.onSeeVideo(DialogResult.this);
                }
            }
        });
        tvReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(DialogResult.this.btnContent)){
                    UMManager.onEvent(activity,"Ad2Close");
                }
                if (clickLister != null) {
                    clickLister.onReceive(DialogResult.this);
                }
            }
        });
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(DialogResult.this.btnContent)){
                    UMManager.onEvent(activity,"Ad2Close");
                }
                if (clickLister != null) {
                    clickLister.onClose(DialogResult.this);
                }
            }
        });
    }

    public String getTvStart() {
        return tvStart.getText().toString().trim();
    }

    public DialogResult then(OnDialogClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public DialogResult setContent(String value) {
        this.content = value;
        return this;
    }

    public DialogResult setBtnContent(String value) {
        this.btnContent = value;
        return this;
    }

    public DialogResult setPos(int value) {
        this.pos = value;
        return this;
    }

    public interface OnDialogClickLister {
        void onClose(Dialog dialog);//关闭弹窗

        void onSeeVideo(Dialog dialog);//看视频多抽一次

        void onReceive(Dialog dialog);//立即领取
    }

    /**
     * 将px转换为dp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}